home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 8.7 KB | 408 lines | [TEXT/MPS ] |
- /*
- File: DebuggingGear.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef THINK_CPLUS
- #ifndef __STDDEF__
- #include <StdDef.h>
- #endif
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #ifndef THREAD_H
- // #include "Thread.h"
- #endif
-
- #ifndef __DISASMLOOKUP__
- #include "DisAsmLookup.h"
- #endif
-
- #ifndef __UNMANGLER__
- #include "Unmangler.h"
- #endif
-
- #ifndef __DEBUGCONSTANTS__
- #include "DebugConstants.h"
- #endif
-
- #ifndef __WRITELINEWINDOW__
- #include "WriteLineWindow.h"
- #endif
-
- #ifndef __DEBUGGINGWINDOWS__
- #include "DebuggingWindows.h"
- #endif
-
- #ifndef __PROCESSES__
- #include <Processes.h>
- #endif
-
- #ifndef __STDIO__
- #include "stdio.h"
- #endif
-
- #ifndef __BOVINESERVER__
- #include "BovineServer.h"
- #endif
-
- /***********************************|****************************************/
-
- #if debug
-
- TDebugFlag keithFlag(1001), steveFlag(1002), addressTypesFlag(1004), debugFlag(1005), chrisFlag(1007);
-
- // These are the various streams to which output can be sent.
- static TWWStreamBuf wwkeithbuf, wwstevebuf, wwChrisBuf;
- debugstream keith ( &wwkeithbuf), steve ( &wwkeithbuf), chris ( &wwkeithbuf);
-
- void BreakStr ( const Str255 str);
-
- #pragma segment DebuggingGear
- #pragma trace off
-
- /***********************************|****************************************/
-
- void FlushDebugBuffers ()
- {
- keith << flush;
- steve << flush;
- chris << flush;
- }
-
- /***********************************|****************************************/
-
- void ShowErrorIf(Boolean showError, const char* text, OSErr err)
- {
- if (showError && err)
- {
- keith << text << err << endl;
- }
- }
-
- /***********************************|****************************************/
-
- #ifndef THINK_CPLUS
-
- inline Boolean odd(long a) { return (a & 1); }
-
- pascal void GetProcName ( const void* pc, StringPtr procName )
- {
- if (pc != NULL && !odd((long) pc))
- {
- void* nextPC;
- Str255 localProcName;
-
- Ptr limit = (Ptr) ((ptrdiff_t) pc + 32767);
- while (!endOfModule( (void*) pc, limit, (char *) &localProcName, &nextPC))
- {
- if (pc >= limit)
- {
- procName[0] = 0;
- return;
- }
- else
- pc = (void *) ((ptrdiff_t) pc + 2);
- }
-
- if ( chrisFlag.Flag ( kDontUnmangleProcNames ) || ( Unmangle ( (char*) procName, (char *) localProcName, 255) < 1 ) )
- PLstrcpy ( procName, localProcName );
- }
- else
- {
- procName[0] = 0;
- }
- }
-
- /***********************************|****************************************/
-
- void StripArgumentsFromProcName(StringPtr procName)
- {
- for (register index = 1; index <= procName[0]; ++index)
- {
- if (procName[index] == '(')
- {
- procName[index+1] = ')';
- procName[0] = (char) (index+1);
- break;
- }
- }
- }
- #endif
-
- /***********************************|****************************************/
-
- static long gLastFreeMemory = 0;
- const long kLowStackSpaceWarningPoint = 8192;
-
- void ShowOftenMemoryInfo(void) {
- long freeMemory = FreeMem();
- long stackSpace = StackSpace();
- if (debugFlag.Flag(1)) {
- gLastFreeMemory = FreeMem();
- keith << "[ '"; OutputOSType(keith, (OSType) '\?\?\?\?') << "' Free:" << freeMemory << " Stack:" << stackSpace << "]" << endl;
- }
- }
-
- /***********************************|****************************************/
-
- ostream& ElapsedTicks ( ostream& s )
- {
- static unsigned long startTicks = 0;
- unsigned long tickNow = TickCount ();
- if ( startTicks == 0 )
- startTicks = tickNow;
- s << (unsigned long) tickNow - startTicks;
- return s;
- }
-
- /***********************************|****************************************/
-
- Boolean StackSpaceIsLow() {
- long stackSpace = StackSpace();
- Boolean result = (stackSpace < kLowStackSpaceWarningPoint);
-
- return result;
- }
-
- /***********************************|****************************************/
-
- void CheckStackSpaceAndGiveWarningIfNecessary(const StringPtr where) {
- if (StackSpaceIsLow()) {
- long stackSpace = StackSpace();
- keith << "[WARNING: Stack space for thread '"; OutputOSType(keith, (OSType) TGetRefCon()); keith << "' " << " is " << stackSpace <<
- " @ " << where << "." << endl;
- }
- }
-
- /***********************************|****************************************/
-
- void NotifyNewDeleteError ( const char* allocator, OSErr error, unsigned long bytes = 0 )
- {
- #if 1
- Str255 message;
-
- message[0] = sprintf ( (char*) message, "*MEM FAIL: \"%s\" size=%d err=%d",
- allocator, bytes, error );
-
- BreakStr ( message );
-
- #else
-
- char s [ 64 ];
- strncpy ( s, "MEM FAIL: ", sizeof ( s ) );
- strncat ( s, allocator, sizeof ( s ) );
- c2pstr(s);
-
- BreakStr((StringPtr) s);
-
- #endif
- }
-
- /***********************************|****************************************/
-
- static void ShowMemoryIfChanged ( long freeMemory, const void* pc, unsigned char direction )
- {
- if ( gLastFreeMemory != freeMemory )
- {
- OSType threadType = TGetRefCon();
-
- if ( threadType != 'MAIN' || chrisFlag.Flag ( kMemoryInfoChangedInMain ) )
- {
- #ifndef THINK_CPLUS
- Str255 procName;
- GetProcName ( pc, procName );
- #else
- Str255 procName = "\p(Proc name not supported) ";
- #endif
- keith << "[" << direction << procName << " '";
- OutputOSType(keith, threadType ) << "' : " << freeMemory;
- keith << " Δ=" << freeMemory - gLastFreeMemory << " @ " << ElapsedTicks << "]" << endl;
- }
-
- gLastFreeMemory = freeMemory;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean gBovineServerWasInitialized = false;
-
-
- #ifndef THINK_CPLUS
-
- #define TNormalMode 1
-
- inline getTMode ( ) {
- return TNormalMode;
- }
-
- inline Boolean ShouldCallBPEPHandlers()
- {
- return gBovineServerWasInitialized && debugFlag.Flag(1) && (getTMode() == TNormalMode);
- }
-
- extern "C" {
-
- void BP(long pc)
- {
- if ( ShouldCallBPEPHandlers() )
- {
- ShowMemoryIfChanged(FreeMem(),(void*) pc,'>');
- }
-
- if (StackSpaceIsLow())
- {
- Str255 procName;
- GetProcName((void*) pc, procName);
- StripArgumentsFromProcName(procName);
- CheckStackSpaceAndGiveWarningIfNecessary(procName);
- }
- }
-
- void EP(long pc)
- {
- if ( ShouldCallBPEPHandlers() )
- ShowMemoryIfChanged(FreeMem(),(void*) pc,'<');
- }
- }
- #endif
-
- /***********************************|****************************************/
-
- #endif // debug ??
-
- #ifndef THINK_CPLUS
-
- Boolean gHighLevelDebuggerRunning = false;
-
- pascal OSErr FindProcessBySignature(OSType sig,
- ProcessSerialNumber& psn,
- FSSpec* fileSpec)
- {
- OSErr err;
-
- ProcessInfoRec info;
-
- psn.highLongOfPSN = 0;
- psn.lowLongOfPSN = kNoProcess;
- do
- {
- err= GetNextProcess(&psn);
- if( err == noErr )
- {
-
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = NULL;
- info.processAppSpec = fileSpec;
-
- err= GetProcessInformation(&psn, &info);
- }
- } while( (err == noErr) && info.processSignature != sig );
-
- if( err == noErr )
- psn = info.processNumber;
-
- return err;
- } // FindProcessBySignature
-
- /***********************************|****************************************/
-
- Boolean IsHighLevelDebuggerInstalled()
- {
- ProcessSerialNumber pPsn; // The process number of the high level
- // debugger if there is one.
-
- // This assumes that the process manager is available...
-
- // Check to see if one of the high level debuggers are running (SourceBug or Sade). If
- // so then we will send debug output to it.
-
- OSErr err = FindProcessBySignature('Objr', pPsn, NULL); // Is SourceBug running?
- if (err != noErr)
- err = FindProcessBySignature('sade', pPsn, NULL); // Is Sade running?
-
- return ((err == noErr) ? true : false);
- }
-
- /***********************************|****************************************/
-
- Boolean IsLowLevelDebuggerInstalled()
- {
- const long* MacJmp = (long*) 0x210;
- return ( * (long *) MacJmp != 0);
- }
-
- /***********************************|****************************************/
-
- void BreakStr (const Str255 str)
- {
- if (IsHighLevelDebuggerInstalled())
- {
- SysBreakStr ( str );
- } else if (IsLowLevelDebuggerInstalled())
- {
- DebugStr ( str );
- } else {
- keith << "DEBUG: " << str << endl;
- }
- }
-
- /***********************************|****************************************/
-
- void BreakStr (const char* str)
- {
- Str255 message;
- BlockMove((Ptr) str, (Ptr) message, str[0]+1);
- c2pstr((char*) message);
-
- if (IsHighLevelDebuggerInstalled())
- {
- SysBreakStr ( message );
- } else if (IsLowLevelDebuggerInstalled())
- {
- DebugStr ( message );
- } else {
- keith << "DEBUG: " << str << endl;
- }
- }
-
- #else
-
- void BreakStr (const Str255 str)
- {
- DebugStr ( str );
- }
-
- void BreakStr ( const char * str )
- {
- Str255 pStr;
-
- pStr[0] = strlen ( str );
- BlockMove ( (Ptr) str, &pStr[1], pStr[0] );
-
- DebugStr ( pStr );
- }
-
- #endif
-
- /***********************************|****************************************/
-
- Boolean BreakOnFailures()
- {
- return (chrisFlag.Flag(kBreakOnFailures));
- }
-
- /***********************************|****************************************/
-